package com.efast.dmom.aps.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.aps.bean.ProductionLine;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.datahandlers.ProductionLineDataHandler;
import com.efast.dmom.aps.service.ProductionLineService;
import com.efast.dmom.aps.util.StringVerifyUtil;
@Service
public class ProductionLineServiceImpl implements ProductionLineService {
@Autowired
private ProductionLineDataHandler productionLineDataHandler;
	@Override
	public ResultBean save(ProductionLine productionLine) {
		ResultBean bean = new ResultBean(true,"新增成功！");
		if (!StringUtil.isNotBlank(productionLine.getInnerData().getName())
				&& !StringUtil.isNotBlank(productionLine.getInnerData().getLineCode())) {
			bean.setSuccess(false);
			bean.setMessage("产线代码不能为空！");
			return bean;
		}
		
		if (!StringVerifyUtil.isstring(productionLine.getInnerData().getLineCode())) {
			bean.setSuccess(false);
			bean.setMessage("产线编码不是由英文、字母、下划线组成!!");
		    return bean;
		   }
		ProductionLine productionLineByName = productionLineDataHandler.getByName(productionLine.getInnerData().getName());
		ProductionLine productionLineByCode = productionLineDataHandler.getByCode(productionLine.getInnerData().getLineCode());
		if (productionLineByName.getInnerData() != null) {
			bean.setSuccess(false);
			bean.setMessage("产线名称不唯一！");
			return bean;
		}
		if (productionLineByCode.getInnerData() != null) {
			bean.setSuccess(false);
			bean.setMessage("车间编码不唯一!");
			return bean;
		}
		productionLineDataHandler.save(productionLine);
		return bean;
	}

	@Override
	public ResultBean update(ProductionLine productionLine) {
		ResultBean resultBean = new ResultBean(true,"工厂修改成功!");
		ProductionLine plantById = productionLineDataHandler.getById(productionLine.getInnerData().getId());
		
		if (plantById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的产线不存在!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(productionLine.getInnerData().getLineCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("产线编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!productionLine.getInnerData().getLineCode().equals(productionLine.getInnerData().getLineCode())
				&& !productionLine.getInnerData().getName().equals(productionLine.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("产线编码、产线名称不可修改!");
			return resultBean;
		}
			
		productionLineDataHandler.update(productionLine);
		return resultBean;
	}

	@Override
	public ResultBean delete(ProductionLine productionLine) {
		ResultBean resultBean = new ResultBean(true,"工厂删除成功!");
		int count = productionLineDataHandler.getProductionLineById(productionLine.getInnerData().getId());
		if (count>0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该产线已被工作中心引用，无法删除!");
			return resultBean;
		}
		productionLineDataHandler.delete(productionLine);
		return resultBean;
	}

	@Override
	public List<ProductionLine> getByWorkshopId(long workshopId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<ProductionLine> getAllProductionLine() {
		// TODO Auto-generated method stub
		return productionLineDataHandler.getAllProductionLine();
	}

	@Override
	public List<Map<String,String>> queryCodeIdNameByWorkshopId(Long workshopId) {
		return productionLineDataHandler.queryCodeIdNameByWorkshopId(workshopId);
	}


}
